//TODO:original version had ignore-error feature
#include "cdefaultdesktoprenderer.h"
#include "scompositecontext.h"
#include "croot.h"

#include <X11/Xregion.h>

#include <buola/gl/ctexture.h>

namespace buola { namespace bwm {

///////////////////////////////////////////////////////////////////
//                                                               //
//                   creation and initialization                 //
//                                                               //
///////////////////////////////////////////////////////////////////

CDefaultDesktopRenderer::CDefaultDesktopRenderer()
{
}

CDefaultDesktopRenderer::~CDefaultDesktopRenderer()
{
}

void CDefaultDesktopRenderer::Render(int pDesktop,ERenderMode pMode)
{
    glVertexAttrib3f(1,1.0,1.0,1.0);
    glVertexAttrib3f(2,0,0,1.0);

    SGLCompositeContext lContext;
    lContext.mDesktop=pDesktop;
    if(gRoot->GetFlag(COMP_ALLDAMAGE))
    {
        lContext.mRemaining=gRoot->mRegions.mDesktop;
    }
    else
    {
        if(gRoot->GetFlag(COMP_DESKTOPDAMAGE))
        {
            lContext.mRemaining|=gRoot->mRegions.mDamage[pDesktop]&gRoot->mRegions.mDesktop;
        }
        if(gRoot->GetFlag(COMP_VIEWPORTDAMAGE))
        {
            lContext.mRemaining|=gRoot->mRegions.mViewportDamage;
        }
        if(lContext.mRemaining.size()>3)
            lContext.mRemaining=lContext.mRemaining.BoundingBox();
        gRoot->mRegions.mPainted|=lContext.mRemaining;
    }
    gRoot->mRegions.mDamage[pDesktop].clear();

    lContext.mPrevTrans=nullptr;

    for(int i=gRoot->mClientsStack.size()-1;i>=0;i--)
    {
        if((pMode&MODE_NODOCK)&&gRoot->mClientsStack[i]->GetWindowType()==gui::EWindowType::DOCK) continue;
        if((pMode&MODE_DOCKONLY)&&gRoot->mClientsStack[i]->GetWindowType()!=gui::EWindowType::DOCK) continue;
        
        gRoot->mClientsStack[i]->Clip(lContext);
    }
    
    for(CClient *lC=lContext.mPrevTrans;lC;lC=lC->mPrevTrans)
    {
        lC->Paint(lContext);
    }
}

/*namespace bwm*/ } /*namespace buola*/ }
